<?php
	require ('../api/config.php');

    $fid=$_GET['fid'];
    $validWidPattern = '/^\d+$/';
    if (!preg_match($validWidPattern, $fid) and $fid!=NULL)  {
    	echo '<script>window.location.href="../";</script>';  exit();
    } 
    
    if (!$fid) {echo '<script> alert("未查询到参数！"); 
            window.location.href="../index.php?fid='.$fid.'"</script>';exit();}

 	if(!$username){echo '<script>alert("请先登陆账号！"); 
		window.location.href="../login.php?fid='.$fid.'"</script>';exit();
 	}
    
    $sql = mysqli_query($conn,"SELECT title FROM gather WHERE fid LIKE '$fid' ");
    $row = mysqli_fetch_array($sql);
    $name = $row['title'];

//查询是否存在此文件夹
$dir='../upfile/'.$fid;
if(is_dir($dir)){}else{
    echo '<script>alert("暂时没有人提交，无法下载压缩包！！");
    	window.location.href="admin.php?fid='.$fid.'"</script>';exit();
}

$pathdir = "../upfile/".$fid."/";  
$zipcreated = "../file/zip/".$name.".zip"; 
$zip = new ZipArchive; 
if($zip -> open($zipcreated, ZipArchive::CREATE ) === TRUE) { 
    $dir = opendir($pathdir); 
    while($file = readdir($dir)) { 
        if(is_file($pathdir.$file)) { 
            $zip -> addFile($pathdir.$file, $file); 
        } 
    } 
    $zip ->close(); 
} 

?>
<html>
	<head>
		<meta charset="utf-8">
		<title>ZIP打包程序 | TOP图片收集管理系统</title>
		<style>
		div{
		    width: auto;
		    text-align: center;
		    height: 100px;
		    border-radius: 20px;
		    background-color: #000;
		    line-height: 100px;
		    font-size: 30px;
		}
		a{
		    color: #fff;
		    text-decoration:none;
		}
		</style>
	</head>
	<body>
	    <div><a href="../file/zip/<?php echo $name.'.zip' ?>" target="is_file">下载【<?php echo $name.'.zip' ?>】</a></div>
	    <a style="color:#000;font-size=:25px">请在一天内下载，否则管理员会清除压缩包！</a>
		</body>
</html>